package cn.hsw.platform.service.impl;

import cn.hsw.platform.core.doc.exception.CheckPermissionException;
import cn.hsw.platform.core.doc.util.AuthUtil;
import cn.hsw.platform.core.base.BaseServiceImpl;
import cn.hsw.platform.core.moudle.setvalue.NeedSetValue;
import cn.hsw.platform.core.util.BCrypt;
import cn.hsw.platform.core.util.Validator;
import cn.hsw.platform.model.UserModel;
import cn.hsw.platform.repository.UserRepository;
import cn.hsw.platform.service.RoleService;
import cn.hsw.platform.service.UserService;
import cn.hsw.platform.vo.UserVO;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户 服务实现
 * @author 李子木
 * @date 2020-10-30
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<UserRepository,UserModel,Long> implements UserService {

    @Autowired
    RoleService roleService;

    @Autowired
    AuthUtil authUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(UserVO userVO) {
        UserModel userModel = BeanUtil.copyProperties(userVO, UserModel.class);
        /*生成密码*/
        String password = BCrypt.hashpw(userModel.getPassword(), BCrypt.gensalt());

        userModel.setPassword(password);

        userModel = this.save(userModel);

        //后台管理新增用户绑定角色
        String[] rolePid = userVO.getRolePid().split(",");
        for (int i = 0; i < rolePid.length; i++) {
            roleService.bind(userModel.getPid(),Long.parseLong(rolePid[i]));
        }
    }

    @Override
    public UserModel findByUserName(String userName) {
        return getRepository().findByUserName(userName);
    }

    @Override
    public UserModel findByPhone(String phone) {
        return getRepository().findByPhone(phone);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(UserModel userModel, String realName, String remark, Long [] rolePid) {
        if(Validator.isNotNull(realName)){
            userModel.setRealName(realName);
        }
        if(Validator.isNotNull(remark)){
            userModel.setRemark(remark);
        }
        this.save(userModel);
        if(rolePid.length>0){
            roleService.unBind(userModel.getPid());
            for (int i = 0; i < rolePid.length; i++) {
                roleService.bind(userModel.getPid(),rolePid[i]);
            }
        }
        authUtil.clearUserPidCache(userModel.getPid());
    }

    @Override
    public void editPassword(UserModel userModel, String password) {
        /*生成密码*/
        String newPassword = BCrypt.hashpw(password, BCrypt.gensalt());

        userModel.setPassword(newPassword);
        this.save(userModel);
        authUtil.clearUserPidCache(userModel.getPid());
    }

    @Override
    public String loginByPassword(String account, String password) {
        UserModel userModel =getRepository().findByUserNameOrPhone(account,account);
        Validator.validateNotNull(userModel,"账号或密码不正确");

        if(BCrypt.checkpw(password,userModel.getPassword())){

            return authUtil.login(userModel.getPid());

        }else{
            throw new CheckPermissionException("账号或密码不正确");
        }
    }

    @Override
    @NeedSetValue
    public Page<UserModel> findByUserNameAndPhoneLike(String userName, String phone, Pageable pageable) {

        return this.getRepository().findByUserNameLikeAndPhoneLike("%"+userName+"%","%"+phone+"%",pageable);
    }

    @Override
    public String resetPassword(UserModel userModel) {


        String newPassword = RandomUtil.randomString("abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-=1234567890",10);
        /*生成密码*/
        String password = BCrypt.hashpw(newPassword, BCrypt.gensalt());

        userModel.setPassword(password);

        userModel = this.save(userModel);
        authUtil.clearUserPidCache(userModel.getPid());
        return newPassword;
    }

    @Override
    @NeedSetValue
    public UserModel findById(Long pid){
        return super.findById(pid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long pid) {
        roleService.unBind(pid);
        super.delete(pid);
        authUtil.clearUserPidCache(pid);
    }
}
